#include "./res/m.h"



// 天气显示模块
char* weather_display(lv_obj_t *win)
{
    // 创建一个按钮
    lv_obj_t *btn = lv_btn_create(win);
    lv_obj_align(btn, LV_ALIGN_LEFT_MID, 40, 0);
    lv_obj_set_size(btn, 300, 130);

    // 创建一个样式
    static lv_style_t style;
    lv_style_init(&style);

    // 设置按钮背景的透明度
    lv_style_set_bg_opa(&style, LV_OPA_60);
    // 设置按钮背景的颜色
    lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_RED));
    // 将样式应用到按钮上
    lv_obj_add_style(btn, &style, LV_PART_MAIN);

    //创建标签显示天气
    lv_obj_t *lable_weather = lv_label_create(btn);
    lv_obj_set_style_text_font(lable_weather, &lv_font_montserrat_32, 0);
    lv_obj_align(lable_weather, LV_ALIGN_CENTER, 0, 20);



    //向聚合数据获取天气信息，需要插网线
    // 创建TCP套接字文件
    int socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if(socketfd == -1)
    {
        perror("socket error");
        return NULL;
    }
 
    // 根据域名 得到 服务器的IP地址
    // 203.107.54.210   
    // struct hostent *info = gethostbyname("apis.juhe.cn");
    // if(info == NULL)
    // {
    //     perror("gethostbyname error");
    //     return;
    // }
    // char ip[100] = {0};
    // strcpy(ip, inet_ntoa(*(struct in_addr*)((info->h_addr_list)[0])));
    // printf("%s\n",ip);
    char ip[100] = {0};
    strcpy(ip, "203.107.54.210");
    printf("%s\n",ip);
    

    // 连接服务器   HTTP协议默认端口号都是80
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(80);
    serveraddr.sin_addr.s_addr = inet_addr(ip);
    int ret = connect(socketfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
    if(ret == -1){
        perror("perror error");
        return NULL;
    }
    printf("connect success\n");

    //发送数据  HTTP请求报文
    char *requestHttp = "GET http://apis.juhe.cn/simpleWeather/query?city=%E5%B9%BF%E5%B7%9E&key=bde27429341f7e2d0ab9beff46f8267a HTTP/1.1\r\n"    
                        "Content-Type:application/x-www-form-urlencoded\r\n"                                        
                        "Host:apis.juhe.cn\r\n\r\n";
    

    ret = write(socketfd, requestHttp, strlen(requestHttp));
    if(ret == -1)
    {
        perror("write error");
        return NULL;
    }

    //5.接收服务器响应报文
    char buf[1024*3] = {0};
    ret = read(socketfd, buf, sizeof(buf));
    if(ret == -1)
    {
        perror("read error");
        return NULL;
    }

    //printf("%s\n",buf);

    //6.关闭文件
    close(socketfd);

    // 分离响应头和JSON部分
    // 查找响应头和正文之间的分隔符
    char *body = strstr(buf, "\r\n\r\n");
    if (body == NULL) {
        fprintf(stderr, "未找到头部和正文的分隔符。\n");
        return NULL;
    }

    // 跳过头部，指向正文开始的位置
    body += 7; // 跳过 "\r\n\r\n"

    printf("body:%s",body);


    //用JSON将数据中的今天的温度/湿度提取出来
    //解析json数据
    //将普通的json文本数据buf 转换成JSON对象
    cJSON *root = cJSON_Parse(body);

    //根据键result 获得 值
    cJSON *result = cJSON_GetObjectItem(root, "result");
    //根据键realtime 获得 值
    cJSON *realtime = cJSON_GetObjectItem(result, "realtime");
    //根据键temperature 获得 温度值
    cJSON *temperature = cJSON_GetObjectItem(realtime, "temperature");
    cJSON *humidity = cJSON_GetObjectItem(realtime, "humidity");
    //验证信息是否有误差,无视此处%警告
    printf("temperature:%s°C\nhumidity:%s%RH\n",temperature->valuestring,humidity->valuestring);
       
    char *pj_buf = malloc(1024 * sizeof(char));
    // 更新标签文本,无视此处%警告
    sprintf(pj_buf,"humidity:%s%RH\ntemperature:%s°C\n", humidity->valuestring, temperature->valuestring);
    lv_label_set_text(lable_weather, pj_buf);
  
    return pj_buf;
}